home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / lisp / indent.el < prev    next >
Lisp/Scheme  |  1992-02-21  |  7KB  |  226 lines

  1. ;; Indentation commands for Emacs
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. ;Now in loaddefs.el
  22. ;(defvar indent-line-function
  23. ;  'indent-to-left-margin
  24. ;  "Function to indent current line.")
  25.  
  26. (defun indent-according-to-mode ()
  27.   "Indent line in proper way for current major mode."
  28.   (interactive)
  29.   (funcall indent-line-function))
  30.  
  31. (defun indent-for-tab-command ()
  32.   "Indent line in proper way for current major mode."
  33.   (interactive)
  34.   (if (eq indent-line-function 'indent-to-left-margin)
  35.       (insert-tab)
  36.     (funcall indent-line-function)))
  37.  
  38. (defun insert-tab ()
  39.   (if abbrev-mode
  40.       (expand-abbrev))
  41.   (if indent-tabs-mode
  42.       (insert ?\t)
  43.     (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
  44.  
  45. (defun indent-rigidly (start end arg)
  46.   "Indent all lines starting in the region sideways by ARG columns.
  47. Called from a program, takes three arguments, START, END and ARG."
  48.   (interactive "r\np")
  49.   (save-excursion
  50.     (goto-char end)
  51.     (setq end (point-marker))
  52.     (goto-char start)
  53.     (or (bolp) (forward-line 1))
  54.     (while (< (point) end)
  55.       (let ((indent (current-indentation)))
  56.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  57.     (or (eolp)
  58.         (indent-to (max 0 (+ indent arg)) 0)))
  59.       (forward-line 1))
  60.     (move-marker end nil)))
  61.  
  62. ;; This is the default indent-line-function,
  63. ;; used in Fundamental Mode, Text Mode, etc.
  64. (defun indent-to-left-margin ()
  65.   (or (= (current-indentation) left-margin)
  66.       (let (epos)
  67.     (save-excursion
  68.      (beginning-of-line)
  69.      (delete-region (point)
  70.             (progn (skip-chars-forward " \t")
  71.                    (point)))
  72.      (indent-to left-margin)
  73.      (setq epos (point)))
  74.     (if (< (point) epos)
  75.         (goto-char epos)))))
  76.  
  77. (defvar indent-region-function nil
  78.   "Function which is short cut to indent each line in region with Tab.
  79. nil means really call Tab on each line.")
  80.  
  81. (defun indent-region (start end arg)
  82.   "Indent each nonblank line in the region.
  83. With no argument, indent each line with Tab.
  84. With argument COLUMN, indent each line to that column.
  85. Called from a program, takes three args: START, END and COLUMN."
  86.   (interactive "r\nP")
  87.   (if (null arg)
  88.       (if indent-region-function
  89.       (funcall indent-region-function start end)
  90.     (save-excursion
  91.      (goto-char end)
  92.      (setq end (point-marker))
  93.      (goto-char start)
  94.      (or (bolp) (forward-line 1))
  95.      (while (< (point) end)
  96.        (funcall indent-line-function)
  97.        (forward-line 1))
  98.      (move-marker end nil)))
  99.     (setq arg (prefix-numeric-value arg))
  100.   (save-excursion
  101.    (goto-char end)
  102.    (setq end (point-marker))
  103.    (goto-char start)
  104.    (or (bolp) (forward-line 1))
  105.    (while (< (point) end)
  106.      (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  107.      (or (eolp)
  108.      (indent-to arg 0))
  109.      (forward-line 1))
  110.    (move-marker end nil))))
  111.  
  112. (defun indent-relative-maybe ()
  113.   "Indent a new line like previous nonblank line."
  114.   (interactive)
  115.   (indent-relative t))
  116.  
  117. (defun indent-relative (&optional unindented-ok)
  118.   "Space out to under next indent point in previous nonblank line.
  119. An indent point is a non-whitespace character following whitespace.
  120. If the previous nonblank line has no indent points beyond
  121. the column point starts at,  tab-to-tab-stop  is done instead."
  122.   (interactive "P")
  123.   (if abbrev-mode (expand-abbrev))
  124.   (let ((start-column (current-column))
  125.     indent)
  126.     (save-excursion
  127.       (beginning-of-line)
  128.       (if (re-search-backward "^[^\n]" nil t)
  129.       (let ((end (save-excursion (forward-line 1) (point))))
  130.         (move-to-column start-column)
  131.         ;; Is start-column inside a tab on this line?
  132.         (if (> (current-column) start-column)
  133.         (backward-char 1))
  134.         (or (looking-at "[ \t]")
  135.         unindented-ok
  136.         (skip-chars-forward "^ \t" end))
  137.         (skip-chars-forward " \t" end)
  138.         (or (= (point) end) (setq indent (current-column))))))
  139.     (if indent
  140.     (let ((opoint (point-marker)))
  141.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  142.       (indent-to indent 0)
  143.       (if (> opoint (point))
  144.           (goto-char opoint))
  145.       (move-marker opoint nil))
  146.       (tab-to-tab-stop))))
  147.  
  148. (defvar tab-stop-list
  149.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  150.   "*List of tab stop positions used by tab-to-tab-stops.")
  151.  
  152. (defvar edit-tab-stops-map nil "Keymap used in edit-tab-stops.")
  153. (if edit-tab-stops-map
  154.     nil
  155.   (setq edit-tab-stops-map (make-sparse-keymap))
  156.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  157.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  158.  
  159. (defvar edit-tab-stops-buffer nil
  160.   "Buffer whose tab stops are being edited--in case
  161. the variable tab-stop-list is local in that buffer.")
  162.  
  163. (defun edit-tab-stops ()
  164.   "Edit the tab stops used by tab-to-tab-stop.
  165. Creates a buffer *Tab Stops* containing text describing the tab stops.
  166. A colon indicates a column where there is a tab stop.
  167. You can add or remove colons and then do C-c C-c to make changes take effect."
  168.   (interactive)
  169.   (setq edit-tab-stops-buffer (current-buffer))
  170.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  171.   (use-local-map edit-tab-stops-map)
  172.   (make-local-variable 'indent-tabs-mode)
  173.   (setq indent-tabs-mode nil)
  174.   (overwrite-mode 1)
  175.   (setq truncate-lines t)
  176.   (erase-buffer)
  177.   (let ((tabs tab-stop-list))
  178.     (while tabs
  179.       (indent-to (car tabs) 0)
  180.       (insert ?:)
  181.       (setq tabs (cdr tabs))))
  182.   (let ((count 0))
  183.     (insert ?\n)
  184.     (while (< count 8)
  185.       (insert (+ count ?0))
  186.     (insert "         ")
  187.       (setq count (1+ count)))
  188.     (insert ?\n)
  189.     (while (> count 0)
  190.       (insert "0123456789")
  191.       (setq count (1- count))))
  192.   (insert "\nTo install changes, type C-c C-c")
  193.   (goto-char (point-min)))
  194.  
  195. (defun edit-tab-stops-note-changes ()
  196.   "Put edited tab stops into effect."
  197.   (interactive)
  198.     (let (tabs)
  199.       (save-excursion
  200.     (goto-char 1)
  201.     (end-of-line)
  202.     (while (search-backward ":" nil t)
  203.       (setq tabs (cons (current-column) tabs))))
  204.       (bury-buffer (prog1 (current-buffer)
  205.               (switch-to-buffer edit-tab-stops-buffer)))
  206.       (setq tab-stop-list tabs))
  207.   (message "Tab stops installed"))
  208.  
  209. (defun tab-to-tab-stop ()
  210.   "Insert spaces or tabs to next defined tab-stop column.
  211. The variable tab-stop-list is a list of columns at which there are tab stops.
  212. Use \\[edit-tab-stops] to edit them interactively."
  213.   (interactive)
  214.   (if abbrev-mode (expand-abbrev))
  215.   (let ((tabs tab-stop-list))
  216.     (while (and tabs (>= (current-column) (car tabs)))
  217.       (setq tabs (cdr tabs)))
  218.     (if tabs
  219.     (indent-to (car tabs))
  220.       (insert ? ))))
  221.  
  222. (define-key global-map "\t" 'indent-for-tab-command)
  223. (define-key esc-map "\034" 'indent-region)
  224. (define-key ctl-x-map "\t" 'indent-rigidly)
  225. (define-key esc-map "i" 'tab-to-tab-stop)
  226.